<?php
namespace app\api\model;
use app\admin\model\Warehouse;
use think\Db;
use traits\model\SoftDelete;

class Shop extends Base
{
    use SoftDelete;
    //联查省
    public function pname(){
        return $this->hasOne("ChinaProvince","id","province")->bind(['province_name'=>'name']);
    }
    //联查市
    public function cname(){
        return $this->hasOne("ChinaCity","id","city")->bind(['city_name'=>'name']);
    }
    //联查区
    public function aname(){
        return $this->hasOne("ChinaArea","id","area")->bind(['area_name'=>'name']);
    }
    public function action_data($dataList){
        foreach ($dataList['data'] as $key=>$value){
            $dataList['data'][$key] = [
                'id'   =>$value['id'],
                'logo' =>$value['logo'],
                'name' =>$value['name'],
                'province_name'=>$value['province_name'],
                'city_name'=>$value['city_name'],
                'area_name'=>$value['area_name'],
                'shop_level'=>$value['shop_level'],
                'order_product_number'=>$value['order_product_number'],
            ];
            $where = [
                ['a.uid','eq',$value['id']],
                ['a.is_publish','eq',2]
            ];
            $dataList['data'][$key]['product'] = (new Product())
                ->alias("a")
                ->join("om_product_price b","a.id = b.product_id and b.type = 2 and b.delete_time is null","left")
                ->limit(3)
                ->order("a.sales_actual_number desc,a.id desc")
                ->field("a.id,a.cover_img,b.price")
                ->where($where)
                ->select();
            foreach ($dataList['data'][$key]['product'] as $ke=>&$val){
                $val['cover_img'] = imgUrlToAbsolute($val['cover_img']);
            }
        }
        return $dataList;
    }
    public function OrderNotify($post){
        $order = (new Shop())->findOne([['out_trade_no','eq',$post['out_trade_no']]]);
        if($order['status']==1){
            $new =[
                'id'=>$order['id'],
                'status'=>2,
                'pay_time'=>time(),
            ];
            Db::startTrans();
            $res =(new Shop())->dataUpdate($new);
            if($res){//修改 商家状态
                if($order['type']==2){ //品牌店加仓库
                    $res= (new Warehouse())->addWarehouse($order['id'],$order['name']);
                    if(!$res){
                        Db::rollback();
                    }
                }
                Db::commit();
            }else{
                Db::rollback();
            }
        }
    }
}